<template>
  <el-dialog :title="!dataForm.id ? '新建' :'编辑'" :close-on-click-modal="false" append-to-body :visible.sync="visible"
    class="JNPF-dialog JNPF-dialog_center" lock-scroll width="600px">
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="120px" label-position="right">
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="基地名称" prop="baseName">
              <el-input v-model="dataForm.baseName" :maxlength="20" placeholder="请输入基地名称" clearable :style='{"width":"100%"}'>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="主导产业" prop="leadIndustry">
              <el-input v-model="dataForm.leadIndustry" :maxlength="10" placeholder="请输入主导产业" clearable :style='{"width":"100%"}'>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="基地状态 " prop="enabledMark">
              <el-select v-model="dataForm.enabledMark" placeholder="请选择基地状态 " clearable :style='{"width":"100%"}'>
                <el-option v-for="(item, index) in enabledmarkOptions" :key="item.enCode" :label="item.fullName"
                  :value="item.enCode" ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="用地面积" prop="landArea">
              <el-input-number v-model="dataForm.landArea" placeholder="用地面积" :precision="2" :step="0.1" >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="建筑面积" prop="buildArea">
              <el-input-number v-model="dataForm.buildArea" placeholder="建筑面积" :precision="2" :step="0.1":max="9999999999" >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="入驻企业数" prop="settledEnNum">
              <el-input-number v-model="dataForm.settledEnNum" placeholder="入驻企业数" :max="9999999999" step-strictly :step="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="科技型企业数" prop="techEnNum">
              <el-input-number v-model="dataForm.techEnNum" placeholder="科技型企业数" :max="9999999999" step-strictly :step="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="专精特新企业数" prop="specNewEnNum">
              <el-input-number v-model="dataForm.specNewEnNum" placeholder="专精特新企业数" :max="9999999999" step-strictly :step="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="规上企业数" prop="standardSizeEnNum">
              <el-input-number v-model="dataForm.standardSizeEnNum" placeholder="规上企业数" :max="9999999999" step-strictly :step="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="就业人数" prop="employedNum">
              <el-input-number v-model="dataForm.employedNum" placeholder="就业人数" :max="9999999999" step-strictly :step="1">
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="年营业收入(万元)" prop="annualIncome">
              <el-input-number v-model="dataForm.annualIncome" placeholder="年营业收入" :precision="2" :step="0.1" :max="9999999999" >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="description">
              <el-input v-model="dataForm.description" placeholder="最多输入300字" :style='{"width":"100%"}' true type="textarea"
                :autosize='{"minRows":4,"maxRows":4}' :maxlength="300" >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
      <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm" ref="selectDialog"
        @select="addForSelect" @close="selectDialogVisible=false" />
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
   import request from '@/utils/request'
  import { getDataInterfaceRes, getDictionaryDataSelector } from '@/api/asso/base/base'
  export default {
    components: {},
    props: [],
    data() {
      return {
        visible: false,
        loading: false,
        btnLoading: false,
        selectDialogVisible: false,
        currTableConf: {},
        addTableConf: {},
        tableRows: {},
        currVmodel: "",
        dataForm: {
          baseName: '',
          leadIndustry: '',
          enabledMark: '0',
          landArea: undefined,
          buildArea: undefined,
          settledEnNum: undefined,
          techEnNum: undefined,
          specNewEnNum: undefined,
          standardSizeEnNum: undefined,
          employedNum: undefined,
          annualIncome: undefined,
          description: '',
          creatoruserid:'',
          creatortime: null,
        },
        rules: {
          baseName: [{
            required: true,
            message: '请输入基地名称',
            trigger: 'blur'
          }],
          enabledMark: [{
            required: true,
            message: '请选择基地状态 ',
            trigger: 'blur'
          }],
        },
        enabledmarkOptions: [{
          fullName: '禁用',
          enCode: 0,
        }, {
          fullName: '启用',
          enCode: 1,
        }],
      }
    },
    computed: {},
    watch: {},
    created() {},
    mounted() {},
    methods: {
      clearData() {
        this.dataForm.baseName = '';
        this.dataForm.leadIndustry = '';
        this.dataForm.enabledMark = '';
        this.dataForm.landArea = undefined,
        this.dataForm.buildArea = undefined,
        this.dataForm.settledEnNum = undefined,
        this.dataForm.techEnNum = undefined,
        this.dataForm.specNewEnNum = undefined,
        this.dataForm.standardSizeEnNum = undefined,
        this.dataForm.employedNum = undefined,
        this.dataForm.annualIncome = undefined,
        this.dataForm.description = '';
        this.dataForm.creatoruserid = "";
        this.dataForm.creatortime = "";
      },
      init(id) {
        this.dataForm.id = id || 0;
        this.visible = true;
        this.$nextTick(() => {
          this.$refs['elForm'].resetFields();
          if (this.dataForm.id) {
            this.loading = true
            request({
              url: '/api/zgyf/BaseManage/' + this.dataForm.id,
              method: 'get'
            }).then(res => {
              this.dataInfo(res.data)
              this.loading = false
            });
          } else {
            this.clearData()
          }
        });
        this.$store.commit('generator/UPDATE_RELATION_DATA', {})
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['elForm'].validate((valid) => {
          if (valid) {
            this.request() 
          }
        })
      },
      request() {
        this.btnLoading = true
        let _data = this.dataList()
        if (!this.dataForm.id) {
          request({
            url: '/api/zgyf/BaseManage',
            method: 'post',
            data: _data
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        } else {
          request({
            url: '/api/zgyf/BaseManage/' + this.dataForm.id,
            method: 'PUT',
            data: _data
          }).then((res) => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.visible = false
                this.btnLoading = false
              }
            })
          }).catch(() => {
            this.btnLoading = false
          })
        }
      },
      openSelectDialog(key) {
        this.currTableConf = this.addTableConf[key]
        this.currVmodel = key
        this.selectDialogVisible = true
        this.$nextTick(() => {
          this.$refs.selectDialog.init()
        })
      },
      addForSelect(data) {
        for (let i = 0; i < data.length; i++) {
          let item = {
            ...this.tableRows[this.currVmodel],
            ...data[i]
          }
          this.dataForm[this.currVmodel].push(item)
        }
      },
      dataList() {
        var _data = JSON.parse(JSON.stringify(this.dataForm));
        return _data;
      },
      dataInfo(dataAll) {
        let _dataAll = dataAll
        this.dataForm = _dataAll
      },
    },
  }
</script>
